cc.Class({
    extends: cc.Component,
    properties: {
        type: 0,
        state: 0,
        index: -1,

        bgValue: 0,// 背景 0是黑的，1是白的
        mainCenterValue: "",
        topRightValue: 0,// 0是不显示，1是显示
        topRightLbValue: 0,
        bottomRightValue: 0,// 0是不显示，1是显示
        bottomRightLbValue: 0,
        bottomLeftValue: 0,// 0是不显示，1是显示
        bottomLeftLbValue: 0,

    },

    onLoad: function () {
        this.init();
    },
    ///由GameView调用控制  初始化角色数据默认值
    init: function () {
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndFunc.bind(this));
    },
    onEnable: function () {
        if (this.type != 1) {
            // 生成背景
            if (this.bgValue == 0) {
                cc.find("bg/bgBlack", this.node).active = true;
            } else {
                cc.find("bg/bgWhite", this.node).active = true;
            }

            if(this.mainCenterValue.length>0){
                cc.find("mainCenter/" + this.mainCenterValue, this.node).active = true;
            }

            if (this.topRightValue == 1) {
                cc.find("topRight", this.node).active = true;
                cc.find("topRight/lbTopRight", this.node).getComponent(cc.Label).string = this.topRightLbValue;
            }
        }

    },

    update: function (dt) {
    },

    changeAppearance: function () {
        this.node.setScale(window.GameConstant.GameDefaultValue.SCALE_MID);
    },
    resetAppearance: function () {
        this.node.setScale(window.GameConstant.GameDefaultValue.SCALE_MAX);
    },
    touchEndFunc: function () {
        if (this.index < 0) {
            console.log("index < 0");
            return;
        }
        if (!this.checkMoveable()) {
            console.log("calced, find not move");
            return
        }

        var preManIndex = window.CardViewData.dynamicData.brave.cmpt.index;
        window.CardViewData.dynamicData.brave.cmpt.index = this.index;

        var tmpNode = cc.instantiate(window.CardViewData.baseCard);
        tmpNode.getComponent("CardComponent").index = preManIndex;

        tmpNode.getComponent("CardComponent").bgValue = 0;
        tmpNode.getComponent("CardComponent").mainCenterValue = preManIndex % 3 == 0 ? "baba" : preManIndex % 3 == 1 ? "biandang" : "mama";
        tmpNode.getComponent("CardComponent").topRightValue = 1;
        tmpNode.getComponent("CardComponent").topRightLbValue = 10;
        

        window.CardViewData.cardArr.splice(preManIndex, 1, tmpNode.getComponent("CardComponent"));
        window.CardViewData.cardArr.splice(this.index, 1, window.CardViewData.dynamicData.brave.cmpt);
        window.CardViewData.dynamicData.cardArrChange = true;

    },
    checkMoveable: function () {
        // 横向投影，纵向投影
        // 如果2边投影之和为1说明可以点击
        var modRowMan = window.CardViewData.dynamicData.brave.cmpt.index % window.CardViewData.rowNum;
        var modRowThis = this.index % window.CardViewData.rowNum;
        var modColMan = parseInt(window.CardViewData.dynamicData.brave.cmpt.index / window.CardViewData.rowNum);
        var modColThis = parseInt(this.index / window.CardViewData.rowNum);
        let result = Math.abs(modRowMan - modRowThis) + Math.abs(modColMan - modColThis);
        return result == 1;
    }
});

